<?php

/**
 * Description of Row
 *
 * @author user3
 */
class Ga_Db_Row extends Zend_Db_Table_Row_Abstract {

    /**
     *
     * @var Ga_Db_Rowset 
     */
    protected $_rowset;

    public function setRowset(Ga_Db_Rowset $rowset)
    {
        $this->_rowset = $rowset;
    }

    private function _initRowset()
    {
        if (!$this->_rowset) {
            $rowsetClass = $this->getTable()->getRowsetClass();
            $data        = array(
                'table' => $this->getTable(),
                'data' => array($this->toArray()),
                'rowClass' => get_class($this)
            );
            $this->_rowset = new $rowsetClass($data);
        }
    }

    /**
     *
     * @param string $dependentTableClass
     * @param Zend_Db_Table_Select $select
     * @return Ga_Db_Rowset
     */
    protected function _findDependentRowset($dependentTableClass, Zend_Db_Table_Select $select = null)
    {
        $this->_initRowset();

        $linkSet = $this->_rowset->getLinkSet();
        $keyLink = Ga_Db_Link::createKey($this->_rowset, $dependentTableClass);
        $link    = $linkSet->getLink($keyLink);

        if (!$link) {
            // create new link ...
            $dependentRowset = $linkSet->findRowset($dependentTableClass);

            if (!$dependentRowset) {

                $dependentTable = new $dependentTableClass();
                if ($select) {
                    $select->setTable($dependentTable);
                } else {
                    $select = $dependentTable->select();
                }

                $reference = $dependentTable->getReference($this->getTableClass());
                $column    = $reference['columns'][0];

                $select->where("$column IN (?)", $this->_rowset->getArrayPrimary());
                $dependentRowset = $dependentTable->fetchAll($select);
            }

            $link = new Ga_Db_Link($this->_rowset, $dependentRowset);
            $linkSet->addLink($link);
        }

        $dependentMap   = $link->getDependentMap();
        $dependentTable = $link->getDependentRowset()->getTable();
        $reference      = $dependentTable->getReference($this->getTableClass());
        $refColumn      = $reference['refColumns'][0];

        $data = array(
            'table' => $dependentTable,
            'data' => isset($dependentMap[$this->$refColumn]) ? $dependentMap[$this->$refColumn] : array(),
            'rowClass' => $dependentTable->getRowClass()
        );

        $rowsetClass  = $dependentTable->getRowsetClass();
        $rowsetResult = new $rowsetClass($data);
        $rowsetResult->setLinkSet($linkSet);

        return $rowsetResult;
    }

    /**
     *
     * @param string $parentTableClass
     * @return Ga_Db_Row 
     */
    protected function _findParentRow($parentTableClass)
    {
        $this->_initRowset();

        $reference = $this->getTable()->getReference($parentTableClass);
        $refColumn = $reference['refColumns'][0];
        $column    = $reference['columns'][0];
        
        $linkSet = $this->_rowset->getLinkSet();
        $keyLink = Ga_Db_Link::createKey($parentTableClass, $this->_rowset);
        $link    = $linkSet->getLink($keyLink);

        if (!$link) {
            // create new link ...
            $parentRowset = $linkSet->findRowset($parentTableClass);

            if (!$parentRowset) {
                $parentTable  = new $parentTableClass();
                $select       = $parentTable->select()->where("$refColumn IN (?)", $this->_rowset->getArrayField($column));
                $parentRowset = $parentTable->fetchAll($select);
            }

            $link = new Ga_Db_Link($parentRowset, $this->_rowset);
            $linkSet->addLink($link);
        }

        $parentMap   = $link->getParentMap();
        $parentTable = $link->getParentRowset()->getTable();
        
        if (!isset($parentMap[$this->$column][0])){
            return null;
        }

        $data = array(
            'table' => $parentTable,
            'data' => $parentMap[$this->$column][0]
        );

        $rowClass  = $parentTable->getRowClass();
        $rowResult = new $rowClass($data);
        return $rowResult;
    }

}